<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="__STATIC__/admin/css/font.css">
        <link rel="stylesheet" href="__STATIC__/admin/css/xadmin.css">
        <script type="text/javascript" src="__STATIC__/admin/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="__STATIC__/admin/js/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                   <div class="layui-form-item layui-form-text">
                      <label for="dcontent" class="layui-form-label">
                          题目
                      </label>
                      <div class="layui-input-block">
                          <textarea placeholder="" id="single_name" name="single_name" class="layui-textarea"></textarea>
                      </div>
                    </div>
                    
                    
                  <div class="layui-form-item">
                      <label for="right_key" class="layui-form-label">
                          <span class="x-red">*</span>正确答案
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="right_key" name="right_key" required="" lay-verify="required"
                          autocomplete="off" class="layui-input">
                      </div> 
                    </div> 
                     <div class="layui-form-item">
                          <label for="username" class="layui-form-label">
                              <span class="x-red">*</span>请选择一个专业</label>
                          <div class="layui-input-inline">
                          <select name="major_id" lay-filter="major" lay-select="" id="major_id" name="major_id">
                            <option value="">请选择一个专业</option>
                            {volist name="major" id="vo"}
                            <option value="{$vo.id}"  {eq name="$Think.get.major_id" value="$vo.id" }selected{/eq} >{$vo.major_name}</option> 
                            {/volist}   
                          </select>  
                      </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red">*</span>请选择一个科目
                        </label>
                        <div class="layui-input-inline">
                            <select name="subject_id" id="subject_id">
                                <option value="">请选择一个科目</option>
                            </select> 
                        </div>
                    </div>

                  <div class="layui-form-item">
                      <label for="L_repass" class="layui-form-label">
                      </label>
                      <button  class="layui-btn" lay-filter="add" lay-submit="">
                          增加
                      </button>
                  </div>
              </form>
            </div>
        </div>
        <script>
          layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                

                //选择二级联动
                form.on('select(major)',function(data){
                    var major_id = $('#major_id').val(); //获取专业表id
                    $.post("{:url('single/major')}",{major_id:major_id},function(data){
                      $("#subject_id").empty();
                      // 清空#subject_id里的元素
                      $.each(data,function(key,val){
                        $("#subject_id").append("<option value="+ val.id + ">" + val.subject_name +"</option>");
                      })
                      form.render('select'); //渲染
                    })
                })


                //监听提交
                form.on('submit(add)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给php
                    $.post("{:url('single/add')}",{data:filterData(data.field)},function (data){
                      if(data.code==1){
                        layer.alert(data.msg, {icon:6},function(){
                          xadmin.close();
                          xadmin.father_reload();
                      });
                    }else{
                      layer.alert(data.msg, {icon: 5},function(){
                        xadmin.close();
                        xadmin.father_reload();
                      });
                    }
                  });
                    return false;
                });
                filterData = function (data) {
                    var roleListIds = {};
                    var temp = {};
                    for (var key in data) {
                        if (key.indexOf("[") === -1) {
                            roleListIds[key] = data[key];
                        } else {
                            temp[key] = data[key];
                        }
                    }

                    var temp_arr = [];
                    for (var key in temp) {
                        var _arr = key.split("[");
                        var field = _arr[0];
                        if (temp_arr.indexOf(field) === -1) {
                            temp_arr.push(field);
                        }
                    }

                    for (var i in temp_arr) {
                        var _f = temp_arr[i];
                        var _temp = [];
                        for (var k in temp) {
                            console.log(k);
                            if (k.indexOf(_f) !== -1) {
                                _temp.push(temp[k])
                            }
                        }
                        roleListIds[_f] = _temp;
                    }
                    return roleListIds;
                };
              });


</script>
    </body>

</html>
